package com.itheima.utils;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.concurrent.TimeUnit;

public class RedisUtil {

    private static RedisTemplate<String,Object> redisTemplate;

    public static void register(RedisTemplate<String,Object> _redisTemplate){
        redisTemplate = _redisTemplate;
        //对redis key值做序列化
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //对redis 的value做序列化
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
    }
    //   存放 秒作为过期时间
    public static  void  set(String key,Object  value,long timeout){
        redisTemplate.opsForValue().set(key,value,timeout, TimeUnit.SECONDS);
    }



    public static void set(String key,Object value){
        redisTemplate.opsForValue().set(key,value);
    }

    public static <T> T get(String key){
        return (T) redisTemplate.opsForValue().get(key);
    }

}
